<?php
include "./_config.php";
class OAuth{
    public static $request_access_token_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc89fd01ce526721c&redirect_uri=[REDIRECT_URI]&response_type=code&scope=[SCOPE]#wechat_redirect";

    public static function getAccessCode($redirect_uri,$scope){
        if(empty($_GET['code'])){
            // 未授权而且是拒绝
            if(!empty($_GET['state'])){
                
            } else {
                // 未授权
                $redirect_uri = urlencode($redirect_uri);
                $RequestUrl = str_replace("[REDIRECT_URI]", $redirect_uri, OAuth::$request_access_token_url);
                $RequestUrl = str_replace("[SCOPE]", $scope, $RequestUrl);
                // 获取授权
                header("location:" . $RequestUrl);
                exit(0);
            }
        } else {
            // 授权成功 返回 access_token 票据
            return $_GET['code'];
        }
    }

    public static function getAccessToken($code){
        $RequestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
        $Result = CurlGet::get($RequestUrl);
        return json_decode($Result,true);
    }
}

class CurlGet{
    
    public static function get($url){
        $ch = curl_init();
　　    //设置选项，包括URL
　　    curl_setopt($ch, CURLOPT_URL, $url);
　　    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
　　    curl_setopt($ch, CURLOPT_HEADER, 0);
　　    //执行并获取HTML文档内容
　　    $output = curl_exec($ch);
　　    //释放curl句柄
　　    curl_close($ch);
　　    //打印获得的数据
　　    return $output;
    }

}
?>